//
//  SendToolBar.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/7/31.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit

enum toolButtonType: Int {
    //图片
    case pictore = 0
    //笑脸
    case smileface
    //键盘
    case keyboard
    //加号
    case add
    //@
    case mention
}

class SendToolBar: UIStackView {
    
    //点击按钮回调闭包
    var buttonClickClusure: ((toolButtonType)->())?
    
    //提取表情键盘
    var faceButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 界面设置
    func setupUI(){
        //设置默认显示方式
        self.axis = .horizontal
        //设置填充方式
        self.distribution = .fillEqually
        
        addButton(imageName: "compose_camerabutton_background",type: .pictore)
        faceButton = addButton(imageName: "compose_emoticonbutton_background",type: .smileface)
        addButton(imageName: "compose_keyboardbutton_background",type: .keyboard)
        addButton(imageName: "compose_add_background", type: .add)
        addButton(imageName: "compose_mentionbutton_background",type: .mention)

    }
    
    //MARK: - 新建toolBar按钮的封装
    @discardableResult //忽略返回值，调用的时候不使用不会有警告
    func addButton(imageName: String,type: toolButtonType)->UIButton{
        let button = UIButton()
        
        button.tag = type.rawValue
        
        button.addTarget(self, action: #selector(clickToolButton(button:)), for: .touchUpInside)
        
        button.setImage(UIImage(named: imageName), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        
        addArrangedSubview(button)
        
        //关闭按钮的点击高亮
        button.adjustsImageWhenHighlighted = false
        
        return button
    }
    
    //MARK: - 按钮点击方法
    func clickToolButton(button: UIButton){
       
        //根据rowValue的值获取枚举值
        let type = toolButtonType(rawValue: button.tag)!
        
        //回调控制器跳转相关页面
        buttonClickClusure?(type)
        
    }
    
    //MARK: - 表情图片与键盘图片的切换
    func swithPictureFaceAndKeyBoard(isFace: Bool){
        if isFace {
            //点击的时笑脸,那就变成键盘
            faceButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            faceButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        }else{
            //点击的时笑脸,那就变成键盘
            faceButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            faceButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
            
        }
    }
}
